<template>
  <el-card class="box-card">
    <template #header>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="盘点计划单">
          <el-input v-model="formInline.inventoryNo" placeholder="请输入盘点计划单" clearable />
        </el-form-item>
        <el-form-item label="盘点时间">
          <datePickerRange v-model:start="formInline.beginTime" v-model:end="formInline.endTime"></datePickerRange>
        </el-form-item>
        <el-form-item label="供应商">
          <selectSupplier v-model="formInline.supplierSn" val="supplierSn"></selectSupplier>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="formInline.status" placeholder="请选择状态">
            <el-option v-for="item in counter.inventoryStatus" :key="item.value" :label="item.name" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">搜索</el-button>
          <el-button type="primary" @click="dialogVisible=true" plain>新增</el-button>
          <el-button @click="init">重置</el-button>
        </el-form-item>
      </el-form>
    </template>

    <el-table :data="tableParams.rows">
      <el-table-column label="盘点计划单" align="center" width="200">
        <template #default="scope">
          <el-button type="primary" text @click="toDetails(scope.row)">{{scope.row.inventoryNo}}</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="inventoryTime" label="盘点时间" />
      <el-table-column prop="address" label="状态">
        <template #default="scope">
          {{counter.idString_format("inventoryStatus",scope.row.status)}}
        </template>
      </el-table-column>
      <el-table-column label="供应商">
        <template #default="scope">
          <selectSupplier v-model="scope.row.supplierSn" val="supplierSn" readonly="1"></selectSupplier>
        </template>
      </el-table-column>
      <el-table-column prop="address" label="工厂">
        <template #default="scope">
          <selectFactory v-model="scope.row.factoryId" readonly="1"></selectFactory>
        </template>
      </el-table-column>
      <el-table-column prop="address" label="仓库">
        <template #default="scope">
          <selectStores v-model="scope.row.storeId" readonly="1"></selectStores>
        </template>
      </el-table-column>
      <el-table-column prop="createBy" label="创建人" />
      <el-table-column prop="createTime" label="创建时间" />
      <el-table-column prop="address" label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" text @click="handClick(scope.row,1)">编辑</el-button>
          <el-button type="primary" text @click="handClick(scope.row,2)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-if="tableParams['total']" :total="tableParams['total']" v-model:page="formInline.pageNum" v-model:limit="formInline.pageSize" @pagination="getInfo" />

    <el-dialog v-model="dialogVisible" title="新增/编辑" width="60%" :before-close="handleClose">
      <el-form :model="form" label-width="100" ref="formEl" :rules="rules">
        <el-form-item label="盘点计划单">
          <el-input v-model="form.inventoryNo" placeholder="请输入盘点计划单" clearable disabled />
        </el-form-item>
        <el-form-item label="盘点时间" prop="inventoryTime">
          <DatePicker v-model="form.inventoryTime"></DatePicker>
        </el-form-item>
        <el-form-item label="工厂" prop="factoryId">
          <selectFactory v-model="form.factoryId"></selectFactory>
        </el-form-item>
        <el-form-item label="仓库" prop="storeId">
          <selectStores v-model="form.storeId"></selectStores>
        </el-form-item>
        <el-form-item label="供应商" prop="supplierSn">
          <selectSupplier v-model="form.supplierSn" val="supplierSn"></selectSupplier>
        </el-form-item>
        <el-form-item label="总重量">
          <el-input v-model="form.totalWeight" placeholder="请输入总重量（KG）" clearable>
            <template #append>KG</template>
          </el-input>
        </el-form-item>
        <el-form-item label="总体积">
          <el-input v-model="form.totalVolume" placeholder="请输入总体积（m³）" clearable>
            <template #append>m³</template>
          </el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark" :autosize="{ minRows: 4 }" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="submitForm" :loading="loading">
            确 认
          </el-button>
        </span>
      </template>
    </el-dialog>
  </el-card>
</template>

<script setup lang="ts">
import { onMounted } from "vue";
//counter
import useCounterStore from "@/store/counter";
const counter = useCounterStore();
const { proxy } = getCurrentInstance();
let dialogVisible = ref(false);
let loading = ref(false);
let formInline = ref({
  pageSize: 10,
  pageNum: 1,
  inventoryNo: "",
  status: "",
  supplierSn: "",
  factoryId: "",
  storeId: "",
  beginTime: "",
  endTime: "",
});
let form = ref({
  inventoryTime: "",
  totalWeight: "",
  totalVolume: "",
  supplierSn: "",
  factoryId: "",
  storeId: "",
  remark: "",
});
let tableParams = ref({});
let rules = ref({
  factoryId: [{ required: true, message: "请选择工厂", trigger: "blur" }],
  inventoryTime: [
    { required: true, message: "请选择盘点时间", trigger: "blur" },
  ],
  storeId: [{ required: true, message: "请选择仓库", trigger: "blur" }],
  supplierSn: [{ required: true, message: "请选择供应商", trigger: "blur" }],
});

onMounted(() => {
  search();
});

let search = () => {
  formInline.value.pageNum = 1;
  getInfo();
};
let getInfo = async () => {
  tableParams.value = await proxy.$apis.inventoryPlanList(formInline.value);
};
let init = () => {
  formInline.value = {
    pageSize: 10,
    pageNum: 1,
    inventoryNo: "",
    status: "",
    supplierSn: "",
    factoryId: "",
    storeId: "",
    beginTime: "",
    endTime: "",
  };
  getInfo();
};
let handleClose = () => {
  form.value = {
    inventoryTime: "",
    totalWeight: "",
    totalVolume: "",
    supplierSn: "",
    factoryId: "",
    storeId: "",
    remark: "",
  };
  proxy.$refs.formEl.resetFields();
  getInfo();
  dialogVisible.value = false;
};
let submitForm = async () => {
  await proxy.$refs.formEl.validate((valid, fields) => {
    if (valid) {
      save();
    } else {
      for (let key in fields) {
        return proxy.$utils.showToast(fields[key][0].message, 0);
      }
    }
  });
};
let save = async () => {
  try {
    loading.value = true;
    if (form.value.id) {
      delete form.value.inventoryNo;
      await proxy.$apis.inventoryPlanPut(form.value);
    } else {
      await proxy.$apis.inventoryPlanAdd(form.value);
    }
    proxy.$utils.showToast("操作成功");
    handleClose();
  } catch (error) {
  } finally {
    loading.value = false;
  }
};
let handClick = async (row, num) => {
  switch (num) {
    case 1:
      form.value = proxy.$utils.formatFunction(row, { ...form.value });
      form.value.id = row.id;
      form.value.inventoryNo = row.inventoryNo;
      dialogVisible.value = true;
      break;
    case 2:
      let resp = await proxy.$utils.confirmTips(`是否确定删除?`);
      if (!resp) return;
      await proxy.$apis.inventoryPlanDel(row.id);
      proxy.$utils.showToast("删除成功");
      getInfo();
      break;
    default:
      break;
  }
};
let toDetails = (e) => {
  proxy.$utils.toLink(`/inventory/inventoryPlanDetails?id=${e.id}`);
};
</script>

<style lang="scss" scoped>
.box-card {
  margin: 20px;
}
</style>
